package com.hyst.web;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.hyst.common.ResultBean;
import com.hyst.common.ResultPage;
import com.hyst.domain.Student;
import com.hyst.domain.StudentView;
import com.hyst.service.StudentService;

/**
 * 免费试听
 * 
 * @author：dy @Date：2018年08月25日
 */
@RestController
@RequestMapping("students")
@CrossOrigin(maxAge = 3600, origins = "*")
public class StudentController {
	@Autowired
	private StudentService studentService;

	@GetMapping
	public ResultPage<StudentView> getListView(final StudentView student) {
		return ResultPage.success(studentService.listview(student));
	}

	// @GetMapping
	// public ResultPage<Student> getProjects(Student student) {
	// return ResultPage.success(studentService.list(student));
	// }

	@PostMapping
	public ResultBean<Student> creat(final Student student) {
		return ResultBean.success(studentService.insert(student));
	}

	/**
	 * @Description: 根据主键查询
	 * @param studentId
	 * @return ResultBean<Student>
	 */
	@GetMapping("{studentId}")
	public ResultBean<Student> getByPriKey(@PathVariable("studentId") final String studentId) {
		try {
			return ResultBean.success(studentService.getByPriKey(studentId));
		} catch (Exception e){
			e.printStackTrace();
		}
		return ResultBean.success(null);
	}


	@GetMapping("saveDelete/{studentId}")
	public ResultBean<Student> saveDelete(@PathVariable("studentId") final String studentId) {
		try {
			return studentService.saveDelete(studentId);
		} catch (Exception e){
			e.printStackTrace();
		}
		return ResultBean.success(null);
	}


	/**
	 * @Description: 根据主键更新全字段
	 * @param student
	 * @return ResultBean<Student>
	 */
	@PatchMapping("{studentId}")
	public ResultBean<Student> update(final Student student) {
		return ResultBean.success(studentService.update(student));
	}

	/**
	 * @Description: 根据主键更新不为默认值的字段
	 * @param student
	 * @return ResultBean<Student>
	 */
	// @PutMapping("{studentId}")
	@PostMapping("{studentId}")
	public ResultBean<Student> updatePatch(@PathVariable("studentId") final String studentId, final Student student) {
		return ResultBean.success(studentService.updateSelective(student));
	}

	/**
	 * @Description: 根据主键删除
	 * @param studentId
	 * @return ResultBean<Student>
	 */
	@DeleteMapping("{studentId}")
	public ResultBean<Student> delete(@PathVariable("studentId") final String studentId) {
		return ResultBean.success(studentService.delete(studentId));
	}

	// 绑定下拉用
	@GetMapping("select2")
	@ResponseBody
	public List<Map> getSelect2Data() {
		return studentService.getSelect2Data();
	}


	// 绑定下拉用
	@GetMapping("select3")
	@ResponseBody
	public List<Map> getSelect3Data() {
		return studentService.getSelect3Data();
	}

	// 绑定下拉用
	@GetMapping("select4")
	@ResponseBody
	public List<Map> getSelect4Data(@RequestParam("key") String key) {
		Map<String, String> first = new HashMap<>();
		first.put("id","all");
		first.put("text","选择学生");
		first.put("showline","选择学生");

		List data = studentService.getSelect4Data(key);
		if(!data.isEmpty()){
			data.add(0,first);
			return data;
		}

		List<Map> select2Data = studentService.getSelect2Data();
		select2Data.add(0,first);
		return select2Data;
	}


	// 条件查询
	@GetMapping("selectByCondition/{condition}")
	public ResultPage<StudentView> getByCondition(final StudentView student,@PathVariable("condition") final String condition) {
		String conditiontest = null;
		try {
			 conditiontest =new String(condition.getBytes("ISO-8859-1"), "utf8");//解决中文格式乱码
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(conditiontest);
		return ResultPage.success(studentService.getDataByCondition(student,conditiontest));
	}
	
	// 条件查询
	@GetMapping("selectByFuzeren/{selectCondition}")
	public ResultPage<StudentView> getByFuzerenId(final StudentView student,@PathVariable("selectCondition") final String fuzerenId) {
		String fuzerenIdtest = null;
		try {
			fuzerenIdtest =new String(fuzerenId.getBytes("ISO-8859-1"), "utf8");//解决中文格式乱码
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(fuzerenIdtest);
		return ResultPage.success(studentService.getDataByFuzerenId(student,fuzerenIdtest));
	}

	@GetMapping("saveAllot/{studentId}")
	public ResultBean<Student> saveAllot(@PathVariable("studentId") final String studentId) {
		System.out.println(studentId);
		return ResultBean.success(studentService.saveAllotData(studentId));
	}
	
//	@GetMapping("saveRemark/{studentId}")
//	public ResultBean<Student> saveRemark(@PathVariable("studentId") final String studentId) {
//		
//		String studentIdtest = null;
//		try {
//			studentIdtest =new String(studentId.getBytes("ISO-8859-1"), "utf8");//解决中文格式乱码
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}		
//		System.out.println(studentIdtest);
//		return ResultBean.success(studentService.saveRemark(studentIdtest));
//	}
	
	@PostMapping("saveRemark")
	public ResultBean<Student> saveRemark(@RequestParam(value = "postMsg") String studentId) {
		
//		String studentIdtest = null;
//		try {
//			studentIdtest =new String(studentId.getBytes("ISO-8859-1"), "utf8");//解决中文格式乱码
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}		
		System.out.println(studentId);
		return ResultBean.success(studentService.saveRemark(studentId));
	}
	
//	@GetMapping("savestateEdit/{studentId}")
//	public ResultBean<Student> savestateEdit(@PathVariable("studentId") final String studentId) {		
//		String studentIdtest = null;
//		try {
//			studentIdtest =new String(studentId.getBytes("ISO-8859-1"), "utf8");//解决中文格式乱码
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}		
//		System.out.println("2018-9-25 chenbin:"+studentIdtest);
//		return ResultBean.success(studentService.savestateEdit(studentIdtest));
//	}
	
	@PostMapping("savestateEdit")
	public ResultBean<Student> savestateEdit(@RequestParam(value = "postMsg") String studentId) {		
//		String studentIdtest = null;
//		try {
//			studentIdtest =new String(studentId.getBytes("ISO-8859-1"), "utf8");//解决中文格式乱码
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}		
		System.out.println("2018-9-25 chenbin:"+studentId);
		return ResultBean.success(studentService.savestateEdit(studentId));
	}
	
	@GetMapping("savedoDelete/{studentId}")
	public ResultBean<Student> savedoDelete(@PathVariable("studentId") final String studentId) {			
		System.out.println(studentId);
		return ResultBean.success(studentService.savedoDelete(studentId));
	}
		
	@GetMapping("info")
	public ResultBean<StudentView> getStuInfo(String studentId) {
		return ResultBean.success(studentService.getStuInfo(studentId));

	}
	
	// 2018-9-25 chenbin: 新增，获取顾问UniqueId
	@GetMapping("getUID")
	public static String getUIDFromSession() {
		String uniqueId = "uniqueId";
		try {
			uniqueId = (String) getRequest().getSession().getAttribute("uniqueid");
		} catch (Exception e) {
			
		}
		return uniqueId;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		return attrs.getRequest();
	}
	@PostMapping("leave")
	//学生请假
	public ResultBean<String> leave(String phone,String startDate,String endDate) throws ParseException, CloneNotSupportedException
	{
		return ResultBean.success(studentService.leave(phone,startDate,endDate));
	}

	@GetMapping("/statics")
	public ResultBean getWeiXinBindStatics() {
		return studentService.weixinBindStatics();
	}
}
